﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
	public partial class Test : System.Web.UI.Page
	{
        string[] arr = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q"
                           ,"R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", ".", "?", "!"};

		protected void Page_Load(object sender, EventArgs e)
		{

		}

        protected void cmdEncode_Click(object sender, EventArgs e)
        {
            string strInput = txtInput.Text.Trim().ToUpper();
            int nResultChar = 0;
            string strResultString = "";
            List<int> list = new List<int>();
            Random random = new Random();

            for (int i = 0; i < strInput.Length; i++)
            {
                int nInputIndex = 0;

                // get char from input string
                string strChar = strInput.Substring(i, 1);

                for (int j = 0; j < 30; j++)
                {
                    if (arr[j] == strChar)
                    {
                        nInputIndex = j;
                        break;
                    }
                }

                // random 0-9 for minimum code
                int nMin = random.Next(1, 9);

                // random 1-10 for character index code multiply
                int nCharIndexMultiply = random.Next(1, 10);

                // random 0-25 for character index code multiply then + minimum code
                int nCharPosition = nInputIndex - nMin + (30 * 1);//nCharIndexMultiply);

                // random 0-9 for minimum code index
                //int nMinIndex = random.Next(0, 9);
                //int nCharIndex = nMinIndex;

                //while (nCharIndex == nMinIndex)
                //{
                //    nCharIndex = random.Next(0, 9);
                //}

                for (int j = 0; j < 10; j++)
                {
                    if (j == 0)
                    {
                        nResultChar = nMin;
                    }
                    else if (j == nMin)
                    {
                        nResultChar = nCharPosition;
                    }
                    else if (j < nMin)
                    {
                        nResultChar = random.Next(nMin + 1, nCharPosition - 1);
                    }
                    else if (j > nMin)
                    {
                        nResultChar = random.Next(nCharPosition + 1, 999);
                    }

                    list.Add(nResultChar);
                }

                //list.Insert(nMinIndex + (10 * i), nMin);
                //list.Insert(nCharIndex + (10 * i), nCharPosition);
            }

            for (int i = 0; i < list.Count; i++)
            {
                strResultString += list[i].ToString() + (i < list.Count - 1 ? " " : "");
            }

            txtInput2.Text = strResultString;
        }

        //==========================================================================================================================================

        private string[] SortArray(string[] array)
        {

            int temp = 0;

            for (int write = 0; write < array.Length; write++)
            {
                for (int sort = 0; sort < array.Length - 1; sort++)
                {
                    if (Convert.ToInt32(array[sort]) > Convert.ToInt32(array[sort + 1]))
                    {
                        temp = Convert.ToInt32(array[sort + 1]);
                        array[sort + 1] = array[sort];
                        array[sort] = temp.ToString();
                    }
                }
            }
            return array;
        }

        public string main()
        {
            string[] splitText = txtInput2.Text.Split(' ');
            string[] array = new string[10];
            int nCurrentIndex = 0;
            string result = "";
            int min = 0;
            int point = 0;

            for (int i = 0; i < splitText.Length; i++)
            {
                array[nCurrentIndex] = splitText[i];
                nCurrentIndex++;

                if (nCurrentIndex == 10)
                {
                    array = SortArray(array);
                    nCurrentIndex = 0;

                    min = Convert.ToInt32(array[0]);
                    point = Convert.ToInt32(array[min]);
                    result += arr[((point + min) % 30)];
                    array = new string[10];
                }
            }

            return result;
        }

        protected void cmdDecode_Click(object sender, EventArgs e)
        {
            lblResult2.Text = main();
        }
	}
}